package com.yc.controller.system.hospital;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yc.controller.base.BaseController;
import com.yc.entity.Page;
import com.yc.service.system.hospital.ICategoryManager;
import com.yc.service.system.hospital.IHospitalManager;
import com.yc.util.Jurisdiction;
import com.yc.util.PageData;


@Controller
@RequestMapping(value="/category")
public class CategoryController extends BaseController {
	
	String menuUrl = "category/getlist.do"; //菜单地址(权限用)
	
	@Resource
	private ICategoryManager categoryManager;
	
	@Resource
	private IHospitalManager hospitalManager;
	
	@RequestMapping(value="/getlist")
	public ModelAndView getHospitalList(Page page)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		String keywords = pd.getString("keywords");				//关键词检索条件
		if(null != keywords && !"".equals(keywords)){
			pd.put("keywords", keywords.trim());
		}
		
		page.setPd(pd);
		List<PageData> list = categoryManager.getCategoryList(page);
		

		mv.setViewName("system/category/category_list");
		mv.addObject("categoryList", list);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
		
	}
	
	@RequestMapping(value="/goAddCategory")
	public ModelAndView goAddHospital() throws Exception{
		
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		Page page = new Page();
		
		List<PageData> list = hospitalManager.getAllHospital(page);

		mv.addObject("hospitalList",list);
		mv.setViewName("system/category/category_edit");
		mv.addObject("action", "saveCategory");
		mv.addObject("pd", pd);

		return mv;
		
	}
	
	@RequestMapping(value="/goEditCategory")
	public ModelAndView goEditHospital() throws Exception{
		
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		pd = categoryManager.findById(pd);	//根据ID读取
		
		Page page = new Page();
		List<PageData> list = hospitalManager.getAllHospital(page);
		mv.addObject("hospitalList",list);
		
		mv.setViewName("system/category/category_edit");
		mv.addObject("action", "editCategory");
		mv.addObject("pd", pd);

		return mv;
		
	}
	
	
	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/editCategory")
	public ModelAndView edit() throws Exception{
		logBefore(logger, Jurisdiction.getUsername()+"修改Category");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		
		pd.put("RCDSTS", "A");
		
		categoryManager.edit(pd);
		
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	
	@RequestMapping(value="/saveCategory")
	public ModelAndView saveHospital() throws Exception{
		logBefore(logger, Jurisdiction.getUsername()+"新增Category");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		pd.put("CATEGORY_ID", this.get32UUID());	//主键
	
		pd.put("RCDSTS", "A");
	
		categoryManager.save(pd);
		
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	/**删除
	 * @param out
	 */
	@RequestMapping(value="/delete")
	public void delete(PrintWriter out) throws Exception{
		logBefore(logger, Jurisdiction.getUsername()+"删除Category");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return;} //校验权限
		PageData pd = new PageData();
		pd = this.getPageData();
		categoryManager.delete(pd);
		out.write("success");
		out.close();
	}
	
	
	/**查看
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/view")
	public ModelAndView view() throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();


		pd = categoryManager.findById(pd);						//根据ID读取
		
		mv.setViewName("system/category/category_view");
		mv.addObject("msg", "editCategory");
		mv.addObject("pd", pd);
		return mv;
	}

}
